const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const HtmlWebpackPlugin = require("html-webpack-plugin");
//const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin')
//用于在构建前清除dist目录中的内容
const CleanWebpackPlugin = require('clean-webpack-plugin');
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const webpackBaseConfig = require("./webpack.config.js");
webpackBaseConfig.plugins = [];
module.exports = merge(webpackBaseConfig, {
	devtool: "source-map",
	output: {
		publicPath: '/',
		filename: './js/[name].[chunkhash:6].js',
		chunkFilename: ('./js/[name].[id].[chunkhash:6].js')
	},
	plugins: [
		//清除dist构建目录文件
		new CleanWebpackPlugin(['dist']),
		new MiniCssExtractPlugin({
			// Options similar to the same options in webpackOptions.output
			// both options are optional
			filename: 'css/[name].[hash:6].css'
		}),

		new OptimizeCssAssetsPlugin({
			assetNameRegExp: /\.css$/g,
			cssProcessor: require('cssnano'),
			cssProcessorPluginOptions: {
				preset: ['default', { discardComments: { removeAll: true } }],
			},
			canPrint: true
		}),

		new webpack.DefinePlugin({
			'process.env': {
				NODE_ENV: '"production"'
			}
		}),


		new HtmlWebpackPlugin({
			filename: 'index.html',
			template: './src/index.html',
			inject: true
		}),
		// keep module.id stable when vendor modules does not change
		new webpack.HashedModuleIdsPlugin(),

		new CopyWebpackPlugin([
			{
				//from: path.join(__dirname, 'static/**/*'),
				//to: 'static/**/*',
			from: 'static/**/*',
			}
		]),

		new VueLoaderPlugin()

	],
	optimization: {
		runtimeChunk: {
			name: 'manifest'
		},
		minimize: true, //是否进行代码压缩
		splitChunks: {
			chunks: 'all',
			minSize: 30000,
			minChunks: 1,
			maxAsyncRequests: 5,
			maxInitialRequests: 3,
			automaticNameDelimiter: '~',
			name: true,
			cacheGroups: {
				vendors: {
					test: /[\\/]node_modules[\\/]/,
					priority: -10
				},
				default: {
					minChunks: 2,
					priority: -20,
					reuseExistingChunk: true
				},

				styles: {
					name: 'common',
					test: /\.(scss|css)$/,
					chunks: 'all',
					reuseExistingChunk: true,
					enforce: true
				}

			}
		}
	}
})
